﻿using System;
using System.Collections.Generic;
using System.Xml;
using Autodesk.Revit.DB;
using YArchitech.Revit;

namespace YArchitech.Plumbing
{
	internal class SystemSetHelper
	{
		public static List<string> readSystemClassAndLevelInfo(string readNodeName)
		{
			List<string> list = new List<string>();
			XmlDocument xmlDocument = new XmlDocument();
			xmlDocument.Load(YJKSystemModule.path);
			foreach (object obj in xmlDocument.SelectSingleNode("configuration").ChildNodes)
			{
				XmlElement xmlElement = (XmlElement)((XmlNode)obj);
				if (xmlElement.Attributes["name"].Value == readNodeName)
				{
					list.Add(xmlElement.Attributes["systemClass"].Value);
					list.Add(xmlElement.Attributes["startLevel"].Value);
					list.Add(xmlElement.Attributes["endLevel"].Value);
				}
			}
			return list;
		}

		public static List<ElementId> getALLELement(Autodesk.Revit.DB.Document doc, string path)
		{
			XmlDocument xmlDocument = new XmlDocument();
			xmlDocument.Load(path);
			XmlNodeList childNodes = xmlDocument.SelectSingleNode("configuration").ChildNodes;
			XmlNodeList xmlNodeList = null;
			foreach (object obj in childNodes)
			{
				XmlElement xmlElement = (XmlElement)((XmlNode)obj);
				if (xmlElement.Attributes["name"].Value == "CheckBoxData")
				{
					xmlNodeList = xmlElement.ChildNodes;
					break;
				}
			}
			int count = xmlNodeList.Count;
			SystemSetStruct[] array = new SystemSetStruct[count];
			for (int i = 0; i < count; i++)
			{
				array[i] = new SystemSetStruct();
			}
			int num = 0;
			foreach (object obj2 in xmlNodeList)
			{
				XmlElement xmlElement2 = (XmlElement)((XmlNode)obj2);
				if (num < count)
				{
					array[num].CategoryName = (BuiltInCategory)Enum.Parse(typeof(BuiltInCategory), xmlElement2.Attributes["BuiltInCategory"].Value, true);
					num++;
				}
			}
			List<ElementId> list = new List<ElementId>();
			for (int j = 0; j < count; j++)
			{
				foreach (Element element in YJKElementFilter.GetElementByCategory(doc, array[j].CategoryName))
				{
					list.Add(element.Id);
				}
			}
			return list;
		}

		public static SystemSetStruct[] read(string readNodeName, string path)
		{
			XmlDocument xmlDocument = new XmlDocument();
			xmlDocument.Load(path);
			XmlNodeList childNodes = xmlDocument.SelectSingleNode("configuration").ChildNodes;
			XmlNodeList xmlNodeList = null;
			foreach (object obj in childNodes)
			{
				XmlElement xmlElement = (XmlElement)((XmlNode)obj);
				if (xmlElement.Attributes["name"].Value == readNodeName)
				{
					xmlNodeList = xmlElement.ChildNodes;
					break;
				}
			}
			int count = xmlNodeList.Count;
			SystemSetStruct[] array = new SystemSetStruct[count];
			for (int i = 0; i < count; i++)
			{
				array[i] = new SystemSetStruct();
			}
			int num = 0;
			foreach (object obj2 in xmlNodeList)
			{
				XmlElement xmlElement2 = (XmlElement)((XmlNode)obj2);
				if (num < count)
				{
					array[num].CategoryName = (BuiltInCategory)Enum.Parse(typeof(BuiltInCategory), xmlElement2.Attributes["BuiltInCategory"].Value, true);
					num++;
				}
			}
			return array;
		}

		public static bool isHaveInTheLevel(XYZ xyz, string level1, string level2)
		{
			bool result = false;
			if (xyz.Z * 0.3048 >= double.Parse(level1) && xyz.Z * 0.3048 <= double.Parse(level2))
			{
				result = true;
			}
			return result;
		}
	}
}
